#include "StdAfx.h"
#include "GTDTreeCtrlItem.h"
#include "GTDTreeCtrl.h"
#include <vector>

GTDTreeCtrlItem::GTDTreeCtrlItem( GTDTreeCtrl* ctrl,HTREEITEM hItem ) 
:m_ctrl(ctrl)
{
	m_hItem = hItem;
}

void GTDTreeCtrlItem::RemoveAllChild()
{
	std::vector<HTREEITEM> vToDel;
	HTREEITEM hChild = GetCtrl().GetChildItem(m_hItem);
	while(hChild)
	{
		hChild = GetCtrl().GetNextSiblingItem(hChild);
	}
	for(size_t i=0;i<vToDel.size();++i)
	{
		GetCtrl().DeleteItem(vToDel[i]);
	}
}

GTDTreeCtrlItem GTDTreeCtrlItem::GetFirstChild()
{
	return GTDTreeCtrlItem(m_ctrl,GetCtrl().GetChildItem(m_hItem));
}

GTDTreeCtrlItem GTDTreeCtrlItem::GetNextSibling()
{
	return GTDTreeCtrlItem(m_ctrl,GetCtrl().GetNextSiblingItem(m_hItem));
}

void GTDTreeCtrlItem::Destroy()
{
	GetCtrl().DeleteItem(m_hItem);
	m_hItem = NULL;
}

void GTDTreeCtrlItem::SetImage( int nImage, int nSelImage )
{
	GetCtrl().SetItemImage(m_hItem,nImage,nSelImage);
}

CString GTDTreeCtrlItem::GetText() const
{
	return	GetCtrl().GetItemText(m_hItem);	
}

void GTDTreeCtrlItem::SetText( const CString& text )
{
	GetCtrl().SetItemText(m_hItem,(LPCTSTR)text);
}

void GTDTreeCtrlItem::SetData( DWORD data )
{
	GetCtrl().SetItemData(m_hItem,data);
}

DWORD GTDTreeCtrlItem::GetData()const
{
	return m_ctrl->GetItemData(m_hItem);
}

GTDTreeCtrlItem GTDTreeCtrlItem::AddChild( const CString& strNode,HTREEITEM HAfter ,int nImage ,int nSelectImage )
{
	HTREEITEM hChild = GetCtrl().InsertItem(strNode,nImage,nSelectImage,m_hItem,HAfter);
	return GTDTreeCtrlItem(m_ctrl,hChild);
}

GTDTreeCtrlItem GTDTreeCtrlItem::AddChild( int StringID,HTREEITEM HAfter ,int nImage ,int nSelectImage)
{
	CString s;
	s.LoadString(StringID);
	return AddChild(s,HAfter);
}

void GTDTreeCtrlItem::Expand()
{
	GetCtrl().Expand(m_hItem,TVE_EXPAND);
}

GTDTreeCtrlItem GTDTreeCtrlItem::GetParent()const
{
	return GTDTreeCtrlItem(m_ctrl,m_ctrl->GetParentItem(m_hItem));
}

void GTDTreeCtrlItem::Select()
{
	m_ctrl->SelectItem(m_hItem);
}
